package com.kosmos.dubhe.wxcp.provider;

import com.kosmos.dubhe.wxcp.setting.WxcpTagScannerSetting;
import com.kosmos.dubhe.wxcp.setting.WxcpUserScannerSetting;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Component;

/**
 * @author wukai
 */
@Slf4j
@Component
@RequiredArgsConstructor
@EnableScheduling
public class WxCpTagScanner implements SchedulingConfigurer {
    private final WxcpTagScannerSetting setting;
    private final WxCpUserProvider provider;

    private static int count = 0;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setTaskScheduler(taskScheduler());
        taskRegistrar.addCronTask(this::fire, setting.getCron());
    }

    public TaskScheduler taskScheduler() {
        ConcurrentTaskScheduler result = new ConcurrentTaskScheduler();
        return result;
    }

    public void fire() {
        if (!setting.isEnable()) {
            return;
        }
        ++count;
        log.info("WxCpUserProvider repeat {} ", count);
        provider.syncUser();
    }
}
